package com.hskn.hss.module.procurementPlan.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.permissionRole.mapper.SysPermissionRoleMapper;
import com.hskn.hss.module.procurementPlan.entity.TlProcurementPlan;
import com.hskn.hss.module.procurementPlan.mapper.TlProcurementPlanMapper;
import com.hskn.hss.module.procurementPlan.service.ITlProcurementPlanService;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-07
 */
@Slf4j
@RestController
@RequestMapping("/hss/tlprocurementplan")
public class TlProcurementPlanController {

    @Autowired
    private ITlProcurementPlanService tlProcurementPlanService;

    /***
     * 新增
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.INSERT, tableName = "tl_procurement_plan", bean = TlProcurementPlan.class, beanMapper = TlProcurementPlanMapper.class, names = "planname")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public AjaxResult<Object> insert(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return tlProcurementPlanService.insert(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    /**
     * 修改
     *
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.UPDATE, tableName = "tl_procurement_plan", idKey = "id", beanMapper = TlProcurementPlanMapper.class, names = "planname")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return tlProcurementPlanService.update(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    /***
     * 删除
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.DELETE, tableName = "tl_procurement_plan", idKey = "id", beanMapper = TlProcurementPlanMapper.class, names = "planname")
    @RequestMapping(value = "deleteByid", method = RequestMethod.POST)
    public AjaxResult<Object> deleteByid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            return tlProcurementPlanService.deleteByid(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    /**
     * 根据Id获取
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getbyid", method = RequestMethod.POST)
    public AjaxResult<Object> getbyid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        TlProcurementPlan TlProcurementPlan = null;
        try {
            TlProcurementPlan = tlProcurementPlanService.getDetailById(params);
            return AjaxResult.success(TlProcurementPlan);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }


    }

    /**
     * 获取列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getlist", method = RequestMethod.POST)
    public AjaxResult<Object> getlist(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            JSONObject json = tlProcurementPlanService.getList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }

    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "tl_procurement_plan", idKey = "id", beanMapper = TlProcurementPlanMapper.class, names = "planname")
    @RequestMapping(value = "audit", method = RequestMethod.POST)
    public AjaxResult<Object> audit(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            return tlProcurementPlanService.audit(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
    }
}

